import axios from 'axios'

let error = console.error

const api = '/products/'

export function save (params, cb, fcb) {
  axios.post(api, params).then((res) => {
    cb(res.data)
  }).catch(error => {
    fcb(error)
  })
}

export function modify (id, params, cb, fcb = () => error('modify')) {
  axios.put(`${api}${id}`, params).then(res => {
    cb(res.data)
  }).catch(error => {
    fcb(error)
  })
}

export function changeEnable (id, enable, cb, fcb = () => error('changeEnable')) {
  axios.put(`${api}${id}/enable?enable=${enable}`).then(res => {
    cb(res.data)
  }).catch(error => {
    fcb(error)
  })
}

export function findAll (params, cb, fcb = () => error('findAll')) {
  axios.get(api, {params}).then(res => {
    let data = res.data
    cb(data)
  }).catch(error => {
    fcb(error)
  })
}

export function findById (id, cb, fcb = () => error('findById')) {
  axios.get(`${api}${id}`).then(res => {
    let data = res.data
    cb(data)
  }).catch(error => {
    fcb(error)
  })
}
